Questions
8 of 24
1How does NestJS resolve constructor injection at runtime?
2What does @Optional() decorator do in NestJS?
3What is the difference between moduleRef.get() and moduleRef.resolve() in NestJS?
4When do you need @Inject(TOKEN) vs plain constructor injection in NestJS?
5What is ModuleRef in NestJS and when would you use it?
6What are the three provider scopes in NestJS and when is each appropriate?
7What is the correct architectural solution for circular dependencies beyond forwardRef()?
8How do you detect and debug circular dependency errors in large NestJS projects?
9What is property-based injection in NestJS and when should you use it?
10How do you write a unit test for a NestJS service that has injected dependencies?
11What is a DI token and what types can be used as tokens in NestJS?
12How do you pass a custom REQUEST object to a REQUEST-scoped provider for queues or CRON jobs in NestJS?
13How does forwardRef() solve circular dependencies in NestJS and how does it work internally?
14How do you implement the Strategy pattern using NestJS DI?
15How do you replace a provider in a NestJS test module using overrideProvider()?
16What is Inversion of Control (IoC) and how does NestJS implement it?
17What is the difference between Dependency Injection (DI) and Inversion of Control (IoC)?
18What is scope propagation in NestJS and why can it impact performance?
19How do you inject the raw HTTP request object inside a REQUEST-scoped provider in NestJS?
20How does moduleRef.resolve() work with scoped providers and what is ContextIdFactory used for?
21What role does reflect-metadata play in NestJS DI?
22What is a circular dependency in NestJS and how does it manifest at runtime?
23What is LazyModuleLoader in NestJS and how does it differ from standard DI?
24What happens to REQUEST-scoped providers during WebSocket connections or microservice message handling in NestJS?
08 / 24

How do you detect and debug circular dependency errors in large NestJS projects?

Read the full startup error trace — NestJS indicates which module or provider index is undefined. Enable verbose logging to see the instantiation order. Use NestJS Devtools for a visual dependency graph. Static analysis tools like dependency-cruiser or eslint-plugin-boundaries can catch cycles before runtime.

Enabling verbose logging to trace bootstrap order
Debugging strategies:
  1. 1

    Read the full error stack — the index number in the error points to the problematic import in your module's imports array.

  2. 2

    Enable verbose NestJS logging to see the module initialization sequence.

  3. 3

    Use @nestjs/devtools-integration for a visual graph of module and provider dependencies.

  4. 4

    Use dependency-cruiser or eslint-plugin-boundaries to enforce architectural rules and catch cycles statically.

  5. 5

    Search your codebase for forwardRef() occurrences — each one is a known cycle that should be documented or fixed.

Difficulty: 6/10
Topics: circular dependencies, NestJS module system, debugging tools

Scenario Questions

0-2 years experience
  1. 1

    You have two NestJS modules, UsersModule and AuthModule, that import each other and cause the application to fail on startup. How would you identify the circular dependency and resolve it?

  2. 2

    When you run npm run start:dev and see an error about a circular dependency in a small NestJS project, what steps do you take to locate the offending providers?

2-5 years experience
  1. 1

    During development of a new feature, you notice that adding a new service to the PaymentsModule triggers a circular dependency error involving the OrdersModule. Walk me through how you would debug this and decide which import to refactor.

  2. 2

    Our CI pipeline started failing with a NestJS circular dependency stack trace after merging a PR that added a shared LoggerService. How would you investigate the root cause and prevent it from recurring?

5-8 years experience
  1. 1

    In a monorepo with dozens of NestJS microservices, circular dependencies have become hard to track. What strategies and tooling would you put in place to detect them early and keep the module graph maintainable?

  2. 2

    Explain how you would redesign a set of interdependent modules to eliminate circular dependencies while preserving lazy loading and performance considerations.

8+ years experience
  1. 1

    Our organization is migrating legacy NestJS code that heavily relies on cross‑module imports, leading to many circular dependencies. How would you plan a phased refactor that minimizes downtime and aligns with long‑term architectural goals?

  2. 2

    Discuss the trade‑offs of using forwardRef versus extracting shared functionality into a core module when trying to break circular dependencies across multiple teams.

Follow-up Questions

  • Which NestJS API or decorator would you use to break a circular import?
  • How can you automate circular‑dependency detection in a CI pipeline?
  • When would you prefer extracting a shared module over using forwardRef?